package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.MchntImageInfoMapper;
import cc.rengu.igas.bomp.common.entity.MchntImageInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/2
 */
public class MchntImageInfoMapperImpl implements MchntImageInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertMchntImageInfo(MchntImageInfo mchntImageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertMchntImageInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntImageInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记商户影像信息失败,instId:<{}>,mchntNo:<{}>,iReturnCode:<{}>", mchntImageInfo.getInstId(), mchntImageInfo.getMchntNo(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateMchntImageInfoById(MchntImageInfo mchntImageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntImageInfoById");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntImageInfo, null);
        if (0 != iReturnCode) {
            rglog.error("更新商户影像信息失败,instId:<{}>,mchntNo:<{}>,id:<{}>,iReturnCode:<{}>", mchntImageInfo.getInstId(), mchntImageInfo.getMchntNo(), mchntImageInfo.getId(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public MchntImageInfo selectMchntImageInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntImageInfoById");
        MchntImageInfo mchntImageInfo = new MchntImageInfo();
        mchntImageInfo.setInstId(instId);
        mchntImageInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntImageInfo, mchntImageInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户影像信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return mchntImageInfo;
    }

    @Override
    public MchntImageInfo selectMchntImageInfoByPrimaryKey(String instId, String mchntNo, String imageType, int imageIndex) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntImageInfoByPrimaryKey");
        MchntImageInfo mchntImageInfo = new MchntImageInfo();
        mchntImageInfo.setInstId(instId);
        mchntImageInfo.setMchntNo(mchntNo);
        mchntImageInfo.setImageType(imageType);
        mchntImageInfo.setImageIndex(imageIndex);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntImageInfo, mchntImageInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户影像信息失败,instId:<{}>,mchntNo:<{}>,imageType:<{}>,imageIndex:<{}>,iReturnCode:<{}>", instId, mchntNo, imageType, imageIndex, iReturnCode);
            return null;
        }
        return mchntImageInfo;
    }

    @Override
    public List<MchntImageInfo> selectMchntImageInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntImageInfoByMchntNo");
        MchntImageInfo mchntImageInfo = new MchntImageInfo();
        mchntImageInfo.setInstId(instId);
        mchntImageInfo.setMchntNo(mchntNo);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntImageInfo, MchntImageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户影像信息失败,instId:<{}>,mchntNo:<{}>", instId, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntImageInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<MchntImageInfo> selectMchntImageInfoByMchntNoAndImageType(String instId, String mchntNo, String imageType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntImageInfoByMchntNoAndImageType");
        MchntImageInfo mchntImageInfo = new MchntImageInfo();
        mchntImageInfo.setInstId(instId);
        mchntImageInfo.setMchntNo(mchntNo);
        mchntImageInfo.setImageType(imageType);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntImageInfo, MchntImageInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取商户影像信息失败,instId:<{}>,mchntNo:<{}>,imageType:<{}>", instId, mchntNo, imageType);
            return null;
        }
        return objectList.stream().map(item -> (MchntImageInfo) item).collect(Collectors.toList());
    }
}
